package by.vkhalko.asamo.dataaccess.impl;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import by.vkhalko.asamo.dataaccess.ContactDao;
import by.vkhalko.asamo.datamodel.Contact;
import by.vkhalko.asamo.datamodel.Contact_;
import by.vkhalko.asamo.datamodel.Person;

@Repository
public class ContactDaoImpl extends AbstractDaoImpl<Long, Contact> implements
		ContactDao {

	protected ContactDaoImpl() {
		super(Contact.class);
	}

	@Override
	public List<Contact> getAllContacts() {
		CriteriaBuilder cBuilder = getEm().getCriteriaBuilder();

		CriteriaQuery<Contact> criteria = cBuilder.createQuery(Contact.class);
		Root<Contact> root = criteria.from(Contact.class);

		criteria.select(root);

		TypedQuery<Contact> query = getEm().createQuery(criteria);
		List<Contact> results = query.getResultList();
		return results;
	}

	@Override
	public List<Contact> getAllByPerson(Person person) {
		return getAllByFieldRestriction(Contact_.person, person);
	}

}
